package com.hq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.bean.Student;
import com.hq.service.StuService;
import com.hq.util.FileUploadUtil;


import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StuController {

    @Resource
    private StuService stuService;

    @RequestMapping("/list.do")
    // @RequestParam(value="参数名称" ，default=“默认值”)
    public String  list(@RequestParam(value="cpage",defaultValue = "1") Integer cpage, Model model,String mohu1,
                        String startTime, String endTime){
        Map map=new HashMap();
        map.put("mohu1",mohu1);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        PageHelper.startPage(cpage,3);
        List list = stuService.list(map);
        PageInfo pi = new PageInfo(list);
        model.addAttribute("pi",pi);
        model.addAttribute("map",map);
        return "list";
    }

    @RequestMapping("toadd.do")
    public String toadd(){
        return "add";
    }

    @RequestMapping("courseList.do")
    @ResponseBody
    public  Object  courseList(){
        List list=stuService.courseList();
        return list;
    }

    @RequestMapping("cityList.do")
    @ResponseBody()
    public Object cityList(Integer pid){
        List list=stuService.cityList(pid);
        return list;
    }

    @RequestMapping("add.do")
    public String add(Student student, Integer[] cids, MultipartFile file, HttpSession session ){
        if(file.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, file, "upload");
            student.setPic("upload/"+upload);
        }
        // 数组转字符串   不会可以放一放
        String join = StringUtils.join(student.getHobbyss(),",");
        student.setHobby(join);


        int i=stuService.addStu(student,cids);
        return "redirect:list.do";
    }

    @RequestMapping("dels.do")
    @ResponseBody
    public Object dels(String ids){
        int i=stuService.dels(ids);
        return i;
    }

    @RequestMapping("/toupd.do")
    public String toupd(Integer sid){
        return "upd";
    }

    @RequestMapping("getInfoByid.do")
    @ResponseBody
    public Object getInfoById(Integer  sid){
        Map map=stuService.getInfoById(sid);
        return map;
    }

    @RequestMapping("upd.do")
    public String  upd(Student student, Integer[] cids, MultipartFile file, HttpSession session ){
        if(file.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, file, "upload");
            student.setPic("upload/"+upload);
        }
        // 数组转字符串   不会可以放一放
        String join = StringUtils.join(student.getHobbyss(),",");
        student.setHobby(join);
       int i= stuService.upd(student,cids);
       return "redirect:list.do";
    }
}
